<?php
$html_head_code['flot_lib'] = '<script type="text/javascript" src="' . __ADMIN_URL__ . substr(dirname(__FILE__) . '/flot/jquery.flot.js', strlen(__ADMIN_DIR__)) . '"/>';
if($p_data){
    $usage["post"] = $p_data;
    if(!empty($p_data['task'])&&!empty($p_data['category'])){
        $CreatedDate = $p_data['date']['CreatedDate'];
        if(empty($CreatedDate)){
            $CreatedDate = time();
        }

        $DoneDate = $p_data['date']['DoneDate'];
        $BegunDate = $p_data['date']['BegunDate'];
        $return = $WideSql->insertToTable("WideDown", array(
            $session->user()->username(),
            $p_data['category'],
            $p_data['task'],
            $CreatedDate,
            $BegunDate,
            $DoneDate

        ), array(
            'CreatedBy',
            'Category',
            'Task',
            'CreatedDate',
            'BegunDate',
            'DoneDate'
        ));
    }

    if(!empty($p_data['delete'])){
        foreach($p_data['delete'] as $p){
            $WideSql->deleteRows("WideDown","ID='$p'");
        }
    }
    if(!empty($p_data['done'])){
        foreach($p_data['done'] as $p ){
            $WideSql->updateRow("WideDown",time(),"DoneDate","ID='$p'");
        }
    }
    if(!empty($p_data['begun'])){
        foreach($p_data['begun'] as $p ){
            $WideSql->updateRow("WideDown",time(),"BegunDate","ID='$p'");
        }
        
    }
    if(!empty($p_data['reopen'])){
        foreach($p_data['reopen'] as $p ){
            $WideSql->updateRow("WideDown",'',"DoneDate","ID='$p'");
        }
        
    }

}
$return = $WideSql->getFromTable("WideDown",NULL,NULL,"CreatedDate","ASC");
if(!$return){
    $return = $WideSql->getError();

} else {
    foreach($return as &$r){
        $data[$r["Category"]][] = $r;
    }

    $dates = dates_inbetween($return[0]['CreatedDate'],time());
    $meta['first'] = $return[0]['CreatedDate'];
    if(!is_numeric($meta['first'])) $meta['first'] = time();

    foreach($data as $key => $d){
        $progress = 0;
        $done = 0;
        foreach($d as $e){
            $dates[date("dmy", $e['CreatedDate'])]['create']++;
            if(!empty($e['DoneDate'])){
                $dates[date("dmy", $e['DoneDate'])]['done']++;
                $done++;
            } else {
                if(!empty($e['BegunDate'])){
                    $progress++;
                    $dates[date("dmy", $e['BegunDate'])]['begun']++;
                }
            }

        }
        $meta[$key] = array (
            'total' => count($d),
            'in_progress' => $progress,
            'done' => $done
            );

    }
}

$i = 0;

foreach($dates as $k => $d){
    if(is_array($d)){
        foreach($d as $q => $val){
            if($q!="unix"){
                $status[$q] += $val;
                $unix = $d['unix']*1000;
                $flot[$q][] = "[$unix,$status[$q]]";
            }
        }
        $i++;
    }
}

foreach($flot as &$s){
    $s = "[".implode(",",$s)."]";
}

$smarty->assign("flot",$flot);
$smarty->assign("meta",$meta);
$smarty->assign("data", $data);

return $smarty->fetch('editor.tpl');
function dates_inbetween($date1, $date2){

    $day = 60*60*24;

    $numbdates = ($date2-$date1)/$day + 1;
    if($numbdates>900){
        $numbdates = 900;
    }
    for($i=0;$i<=$numbdates;$i++){
        $unix = $date1+($day*$i);
        $dates[date("dmy",$unix)] = array('done'=>0,'create'=>0,'begun'=>0, 'unix'=>$unix);
    }

    return $dates;
}
?>
